package com.hnichr.stop.message.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hnichr.share.OpenShare;
import com.hnichr.share.SnapshotShare;
import com.hnichr.stop.message.MessageArrivedHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.Date;

@Component
public class OpenMessageArrivedHandle implements MessageArrivedHandle {
    private static final Logger logger = LoggerFactory.getLogger(OpenMessageArrivedHandle.class);

    @Override
    public boolean support(String topic, String message) {
        return "b956ba98-988cd83e/device/message/down/gpio_out/reply".equals(topic);
    }

    @Override
    public void handle(String topic, String message) {
        try{
            JSONObject jsonObject = JSON.parseObject(message);
            String code = jsonObject.get("code").toString();
            synchronized (OpenShare.getInstance()){
                OpenShare.getInstance().setCode(code);
                OpenShare.getInstance().notifyAll();
            }
        }
        catch (Exception e){
            logger.error("SnapshotMessageArrivedHandle报错",e);
        }


    }
}
